﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace wd_37_combobox_v1
{
    /// <summary>
    /// https://blog.csdn.net/youshuai001/article/details/95028357
    /// 
    /// 
    /// IsTextSearchEnabled = true.
    //  IsEditable = true可编辑状态
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public static string db_file = "city3.db3";
        public static string tb_name = "cityid";

        public MainWindow()
        {
            //先从Nuget处下载SQLite组件
            InitializeComponent();

            //cmb_city.Text = "15239151000";  //初始化

            List<City> list1 = new List<City>();
            
            list1.Add(new City { Vid = 1, Vname = "上海" });
            list1.Add(new City { Vid = 2, Vname = "北京" });
            list1.Add(new City { Vid = 3, Vname = "广州" });

            cmb_list.ItemsSource = list1;
            cmb_list.SelectedIndex = 0;

            // list2
            if (!File.Exists(db_file))
            {
                try
                {
                    DBOpt.CreateDB(db_file);
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            //             try
            //             {
            //                 DBOpt.DeleteTable1(db_file, tb_name);
            //             }
            //             catch (System.Exception ex)
            //             {
            //                 MessageBox.Show(ex.Message);
            //             }

            try
            {
                DBOpt.CreateTable2(db_file, tb_name);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            try
            {
                //DBOpt.QueryData1(tb_name, db_file);

                var ret = DBOpt.QueryData2(tb_name, db_file);   //return tuple

                foreach (string s in ret.Item1)
                {
                    // 不需要遍历list
                }
                cmb_city.ItemsSource = ret.Item1;   //绑定list
                cmb_city.SelectedIndex = 1;     // 默认选中
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void cmb_list_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        public class City
        {
            public int Vid { get; set; }
            public string Vname { get; set; }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string text = cmb_city.Text;

            string[] arr1 = { "tel" };
            string[] arr2 = { text };
            try
            {
                DBOpt.Insert1(arr1, arr2, tb_name, db_file);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }            
        }
    }
}
